package com.example.nonacademic_education.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.captcha.generator.RandomGenerator;
import com.example.nonacademic_education.constant.KapchaCode;
import com.example.nonacademic_education.util.IpUtil;
//import com.google.code.kaptcha.Constants;
//import com.google.code.kaptcha.Producer;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("captcha")
public class CaptchaController {
//    @Resource
//    private Producer captchaProducer;

//    @Resource
//    private Producer captchaProducerMath;
    @Resource
    private KapchaCode kapchaCode;
    @Resource
    private IpUtil ipUtil;

    /**
     * 验证码生成
     */
    @GetMapping(value = "image")
    @SaIgnore
    public void getKaptchaImage(HttpServletRequest request,HttpServletResponse response) {
        //定义图形验证码的长、宽、验证码字符数、干扰元素个数
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 90, 4, 100);
        lineCaptcha.setGenerator(new RandomGenerator("0123456789", 4));
        // 验证码值
        String code = lineCaptcha.getCode();
        //System.out.println("验证码值：" + code);
//        String codeId = ipUtil.getIpAddr(request);
//        if (codeId==null||codeId.equals("")){
//            return;
//        }
        // 模拟把验证码的值存储到缓存(记得添加过期时间)
        kapchaCode.getCodes().put(code, System.currentTimeMillis());
        //System.out.println(codeId+" "+code);
        // 输出到客户端
        try (ServletOutputStream outputStream = response.getOutputStream()) {
            // 图形验证码写出，写出到流
            lineCaptcha.write(outputStream);
        } catch (Exception e) {
            System.out.println("图形验证码输出错误");
        }
//        ServletOutputStream out = null;
//        try {
//            response.setDateHeader("Expires", 0);
//            response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
//            response.addHeader("Cache-Control", "post-check=0, pre-check=0");
//            response.setHeader("Pragma", "no-cache");
//            response.setContentType("image/jpeg");
////            String type="char";
////            if (System.currentTimeMillis()%2==0){
////                type="math";
////            }
//            String capStr = null;
//            String code = null;
//            BufferedImage bi = null;
////            if ("math".equals(type)){//验证码为算数 8*9 类型
//                String capText = captchaProducerMath.createText();
//                capStr = capText.substring(0, capText.lastIndexOf("@"));
//                code = capText.substring(capText.lastIndexOf("@") + 1);
//                bi = captchaProducerMath.createImage(capStr);
////            }else{//验证码为 abcd类型
////                capStr = code = captchaProducer.createText();
////                bi = captchaProducer.createImage(capStr);
////            }
//            //System.out.println(kapchaCode.getCodes());
//            //System.out.println(code);
//            System.out.println(ipUtil.getIpAddr(request));
//            kapchaCode.getCodes().put(ipUtil.getIpAddr(request), code);
//            out = response.getOutputStream();
//            ImageIO.write(bi, "jpg", out);
//            out.flush();
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            try {
//                if (out != null) {
//                    out.close();
//                }
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//        return null;
    }
}
